<template>
  <div>
    <!-- 面包屑导航栏 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>
        <a href="/">权限管理</a>
      </el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片内容区域 -->
    <el-card class="box-card">
      <!-- 添加角色按钮区域 -->
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>
      <!-- 用户列表区 -->
      <el-table :data="rolesRoles" border stripe>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row
              :class="['bdbotton', i1 === 0 ? 'bdtop': '','vibliu']"
              v-for="(item1, i1) in scope.row.children"
              :key="item1.id"
            >
              <!-- 一级 -->
              <el-col :span="5">
                <el-tag>{{item1.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 二级三级 -->
              <el-col :span="19">
                <el-row
                  :class="[in2 === 0 ? '': 'bdtop', 'vibliu']"
                  v-for="(item2,in2) in item1.children"
                  :key="item2.id"
                >
                  <el-col :span="6">
                    <el-tag closable type="success">{{item2.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      closable
                      @close="removeRightById(scope.row, item3.id)"
                      type="warning"
                      v-for="item3 in item2.children"
                      :key="item3.id"
                    >{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <!-- <pre>{{scope.row}}</pre> -->
          </template>
        </el-table-column>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
            <el-button
              size="mini"
              type="warning"
              icon="el-icon-setting"
              @click="showSetRightDialog(scope.row)"
            >分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分配角色权限控件 -->

      <el-dialog title="分配角色" :visible.sync="setRightDialogVisible" width="50%" @close="endir">
        <!-- 树形控件 -->
        <el-tree
          :data="rightslist"
          :props="treeprops"
          node-key="id"
          show-checkbox
          default-expand-all
          :default-checked-keys="defKeys"
          ref="rightsDialogRef"
        ></el-tree>

        <span slot="footer" class="dialog-footer">
          <el-button @click="setRightDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="allotRights">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>


<script>
import { constants } from 'crypto'
export default {
  data() {
    return {
      rolesRoles: [],
      //获取所有权限的数据

      rightslist: [],
      //树形控件的属性绑定对象
      treeprops: {
        label: 'authName',
        children: 'children'
      },

      //控制分配角色的显示与隐藏
      setRightDialogVisible: false,
      // 默认选中的节点Id值数组
      defKeys: [],
      roleId: ''
    }
  },
  created() {
    this.getRolesRoles()
  },
  methods: {
    //获取所有角色列表
    async getRolesRoles() {
      const { data: res } = await this.$http.get('roles')
      //   console.log(res)
      this.rolesRoles = res.data
    },
    //点击删除用户权限
    async removeRightById(role, rightId) {
      //   console.log(id)
      const comfrom = await this.$confirm(
        '此操作将永久删除该文件, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      if (comfrom !== 'confirm') {
        return this.$message.error('删除失败')
      }
      //   console.log('删除成功')
      //   console.log(comfrom)
      const { data: res } = await this.$http.delete(
        `roles/${role.id}/rights/${rightId}`
      )
      if (res.meta.status !== 200) {
        return this.$message.error('删除失败')
      }
      //删除成功后会重新涮新页面，用户体验不好，
      //   this.getRolesRoles()
      //删除成功后组织页面再次刷新，所以这里调用来role.children
      role.children = res.data
    },
    //// 展示分配权限的对话框
    async showSetRightDialog(role) {
      this.roleId = role.id

      const { data: res } = await this.$http.get('rights/tree')
      //   console.log(res)

      //
      this.rightslist = res.data
      this.getdefkeys(role, this.defKeys)
      this.setRightDialogVisible = true
      //   console.log(role)
    },
    //点击分配权限对话框，获取到所分配到的权限id
    getdefkeys(node, arr) {
      if (!node.children) {
        return arr.push(node.id)
      }
      node.children.forEach(item => this.getdefkeys(item, arr))
    },
    //点击关闭对话框给予重置
    endir() {
      this.defKeys = []
    },
    //点击获取id 分配权限
    async allotRights() {
      const keys = [
        //这里用到了Es6中的扩展运算符  /是给数组进行了合并
        ...this.$refs.rightsDialogRef.getCheckedKeys(),
        ...this.$refs.rightsDialogRef.getHalfCheckedKeys()
      ]
      // console.log(keys)

      //以逗号拼接字符串
      const idStr = keys.join(',')
      // console.log(idStr)
      const { data: res } = await this.$http.post(
        `roles/${this.roleId}/rights`,
        { rids: idStr }
      )
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('分配权限失败')
      }
      // console.log('分配权限成功')
      //分配成功之后重新刷新
      this.getRolesRoles()
      this.$message.success('分配权限成功')
      //分配权限成功之后关闭对话框
      this.setRightDialogVisible = false
    }
  }
}
</script>

<style lang="less" scoped>
.el-tag {
  margin: 7px;
}
.bdtop {
  border-top: 1px solid #ccc;
}
.bdbotton {
  border-bottom: 1px solid #ccc;
}
.vibliu {
  display: flex;
  align-items: center;
}
</style>